package com.bangyu.monitor.core.util;

import com.bangyu.monitor.core.common.constant.Const;
import com.bangyu.monitor.modular.system.entity.DeviceIndicator;
import com.bangyu.monitor.modular.system.entity.OriginalData;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;

import java.util.List;
import java.util.Map;

/**
 * @Auther: zhangxue
 * @Date: 19/11/13 22:17
 * @Description:
 */
@Slf4j
public class InfluDbRepositoryUtil {

    public void batchInsertIndicator(List<DeviceIndicator> indicatorList){
        if(CollectionUtils.isNotEmpty(indicatorList)){
            BatchPoints batchPoints = InfluxDbUtil.createBatchPoints(Const.INFLUX_DB_MONITOR);
            for(DeviceIndicator indicator:indicatorList){
                Point point = indicatorData2Point(indicator);
                batchPoints.point(point);
            }
            InfluxDbUtil.batchInsert(batchPoints);
        }
    }


    private  Point indicatorData2Point(DeviceIndicator data){

        Map<String, String> tags = Maps.newHashMap();
        Map<String, Object> fields = Maps.newHashMap();

        tags.put("device_no",data.getDeviceNo());

        fields.put("load_value",data.getLoadValue());
        fields.put("relative_displacement",data.getRelativeDisplacement());
        fields.put("standard_value",data.getStandardValue());
        fields.put("recovery_time",data.getRecoveryTime());
        fields.put("load_limit",data.getLoadLimit());
        return InfluxDbUtil.createPoint(data.getDeviceNo(),tags,fields,data.getTime());
    }


}
